package com.fs.zx.common.starter.util;

import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Objects;

public class ZxBeanUtils {

    /**
     * 是否为基础类
     *
     * @return
     */
    public static boolean isSimpleProperty(Class clazz) {
        return org.springframework.beans.BeanUtils.isSimpleProperty(clazz);
    }

    public static <T> T copy(Object src, Class<T> targetClass) {
        if (Objects.isNull(src)) {
            return null;
        }
        try {
            return JsonUtils.deserialize(JsonUtils.serialize(src), targetClass);
        } catch (Exception e) {
        }
        return null;
    }

    public static <T> T copy(Object src, TypeReference<T> typeReference) {
        if (Objects.isNull(src)) {
            return null;
        }
        try {
            String str = JsonUtils.serialize(src);
            return JsonUtils.deserializeAny(str, typeReference);
        } catch (Exception e) {
        }
        return null;
    }

    public static <T> List<T> copyList(List srcList, Class<T> clazz) {
        if (Objects.isNull(clazz)) {
            return Lists.newArrayList();
        }
        if (CommonUtils.isEmpty(srcList)){
            return Lists.newArrayList();
        }
        try {
            return JsonUtils.deserializeList(JsonUtils.serialize(srcList), clazz);
        } catch (Exception e) {
        }
        return Lists.newArrayList();
    }

}
